package com.atguigu.base;

import java.sql.*;
import java.util.Scanner;

public class JDBCQuick {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取连接对象
        String url = "jdbc:mysql://localhost:3306/atguigu";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);

        //3.获取执行sql语句的对象
        PreparedStatement ps = conn.prepareStatement("select * from t_emp WHERE emp_name = ?");
        System.out.println("请输入要查询的名字:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();

        //4.编写sql语句，并执行
        ps.setString(1, name);
        ResultSet rs = ps.executeQuery();

        //5.处理结果
        while (rs.next()) {
            int empId = rs.getInt("emp_id");
            String empName = rs.getString("emp_name");
            double empSalary = rs.getDouble("emp_Salary");
            int empAge = rs.getInt("emp_Age");
            System.out.println(empId + "\t" + empName + "\t" + empSalary + "\t" + empAge);
        }

        //释放资源
        rs.close();
        ps.close();
        conn.close();
    }
}
